(()=>{
    var app = new Vue({
        el:"#header",
        data:{
            address:[]//储存地址类表
            ,goods_info:goods_info//储存订单列表
            ,goods_money_sum:0//储存总价格
            ,catrid:''//储存id
        }
    });
    //计算总价格
    $.each(app.goods_info,function (i,n) {
        app.goods_money_sum += (n.catrnum* n.goodscompnum[1]);
        //拼接购物车id
        if(i == 0){
            app.catrid = n.catrid
        }else{
            app.catrid+=','+n.catrid
        }
    });
    //点击切换地址
    app.update_address=(index)=>{
        $.each(app.address,function (i,n) {
            app.address[i].type = 0;
        });
        app.address[index].type = 1;
        $.post(DUSER+'/update_default',{id:app.address[index].id});
    };

    //修改收货地址
    app.update = (index)=>{
        layer.open({
            type:2,
            title:false,
            closeBtn:false,
            area:['500px','360px'],
            content:DUSER+'/update_address/id/'+app.address[index].id,
            end:function () {
                get_address();
            }
        })
    };

    //添加收货地址事件 加载添加收货地址页面
    app.add_address = ()=>{
        layer.open({
            type:2,
            title:false,
            closeBtn:false,
            area:['500px','360px'],
            content:DUSER+'/add_address',
            end:function () {
                get_address();
            }
        })
    };

    //初始化获取地址
    get_address();
    function get_address() {
        $.getJSON(`${DUSER}/address`,{data:1},(da)=>{
            app.address = da;
        })
    }

    //验证选择地址
    $('#form').Validform({
        tiptype:function (msg,o,cssctl) {
               if(o.type == 3){
                   layer.msg('请选择地址',{icon:2});
               }
        },
        datatype:{
            select_one:function (g,o,c,r) {
                return o.parents('.address_list').find('input:checked').length >0?true:false;
            }
        },
        beforeSubmit:function (curform) {/*验证成功表单提交前*/
            layer.load(1,{shade:['0.5','#333']});
        },
        ajaxPost:true,
        callback:function (da) {
            location.href=DORDER+'/password/orderId/'+da;
        }
    });
})();